import axios from 'axios'
import { Message } from 'element-ui'
import query from 'querystringify'
// import Cookies from 'js-cookie'
// import { getQueryString } from "@/utils/utils"

const service = axios.create({
  baseURL: '/api/private/v1',
  timeout: 8000
})

service.interceptors.response.use(
  response => {
    console.log(response)
    const { data } = response
    // const { config: { url } } = response

    // if (data.code === '200' || data.status === 0) {
    //   return data.data
    // }
    console.log(data.meta.status)
    if (data.meta.status === 200 || data.meta.status === 201) {
      // Message({
      //   message: data.meta.msg,
      //   type: 'success'
      // })
      return data
    }

    // if (data.code === '1003') {
    //   Message({
    //     message: data.msg,
    //     type: 'error',
    //     duration: 1500,
    //     onClose: () => {
    //       location.href = '/'
    //     }
    //   })
    //   return Promise.reject(data.msg)
    // }

    // if (data.code === '500') {
    //   if (url && url.includes('user/info')) {
    //     Cookies.remove('token')
    //     location.href = '/'
    //   } else {
    //     Message({
    //       message: data.msg,
    //       type: 'error',
    //       duration: 3000
    //     })
    //   }
    //   return Promise.reject(data.msg)
    // }

    // if (data.code === '-1') {
    //   // eslint-disable-next-line prefer-promise-reject-errors
    //   return Promise.reject('用户名已存在')
    // }

    if (data.meta.status !== 200) {
      Message({
        message: data.meta.msg,
        type: 'error'
      })
    }

    return Promise.reject(data.msg)
  },
  error => {
    if (error.code !== 'ECONNABORTED') {
      Message({
        message: error.message,
        type: 'error',
        duration: 5 * 1000
      })
    }
    return Promise.reject(error)
  })
// axios请求拦截 做一个请求头预处理过程
service.interceptors.request.use(config => {
  console.log(config)
  const token = sessionStorage.getItem('token')
  if (token) {
    config.headers.Authorization = token
  }
  // 必须retrun config 回去
  return config
})

const post = (url) => (data, callback) => {
  return service({
    url,
    method: 'POST',
    data,
    onUploadProgress: function (progressEvent) {
      if (callback) {
        callback.onUploadProgress(progressEvent)
      }
    }
  })
}
const postUrl = (url) => (params) => {
  return service({
    url,
    method: 'POST',
    params
  })
}
const postbystring = (url) => (data) => {
  return service({
    url,
    method: 'POST',
    data: query.stringify(data)
  })
}

const get = (url) => (params) => {
  return service({
    url,
    params,
    method: 'GET'
  })
}

const getQuery = (url) => {
  return service({
    url,
    method: 'GET'
  })
}
// put 请求
const putUrl = (url) => (params)=> {
  return service({
    url,
    method: 'PUT',
    params 
  })
}
/**
 * delete
 * @param {String} url [请求的url地址]
 * @param {Object} params [请求时携带的参数]
 */
 const deletefn = (url) => (params) => {
  return service({
    url,
    method: 'DELETE',
    params:params
  })
};

export {
  post,
  postbystring,
  get,
  postUrl,
  putUrl,
  getQuery,
  deletefn
}
